Uec3.ReceiveItemBrowserWindow = Ext.extend(Ext.app.Module, {
   id: 'receive_item_browser',
   type: 'common/notepad',//?
  
   getPanel:function(){
	  		
		var ds_source_select = new Ext.data.JsonStore({
     		url:Uec3.App.Constants.homeUrl+"/dictionary/findByVarGroup.do?group=part_source",
     		root: "",
     		//autoLoad :true,
    		 fields:[{name:'varIndex',type:'int'},{name:'varName',type:'string'}]
 		});  
		
 		var ds_publicanLevel_select = new Ext.data.JsonStore({
     		url:Uec3.App.Constants.homeUrl+"/dictionary/findByVarGroup.do?group=partproperty_publicanLevel",
    	    root: "",
     		//autoLoad :true,
     		fields:[{name:'varIndex',type:'int'},{name:'varName',type:'string'}]
 		});
 
 	/*  var ds_storeId_select = new Ext.data.JsonStore({
   	      url:Uec3.App.Constants.homeUrl+"/store/base!findStoreCombo.do",
   	      root: "root",
   	      autoLoad :true,
   	      fields:[{name:'varIndex',type:'int'},{name:'varName',type:'string'}]
   	  });*/
 		
		var source_comboBox= new Ext.form.ComboBox({
  			typeAhead: true,
  			fieldLabel:'来源',
  			width : 120,
  			valueField:'varIndex',
  			displayField : 'varName',
	   		mode : 'remote',
  			hiddenName:'source',
  			store : ds_source_select,
  			forceSelection: true,
  		    selectOnFocus : true,
  			triggerAction : 'all',
  			allowBlank : true,
  			loadingText : '加载中...',
  			editable:true,
 			emptyText : '请选择',
 			anchor:'100%'
	 	});
	 	
	  var publicanLevel_comboBox= new Ext.form.ComboBox({
  		  typeAhead: true,
  		  width : 120,
          valueField:'varIndex',
          displayField : 'varName',
	      mode : 'remote',
	      hiddenName:'publicanLevel',
         store : ds_publicanLevel_select,
         selectOnFocus : true,
         triggerAction : 'all',
         allowBlank : true,
         forceSelection: true,
         loadingText : '加载中...',
         editable:true,
         emptyText : '请选择',
         valueNotFoundText:''
   });

 /* var storeId_comboBox= new Ext.form.ComboBox({
  		typeAhead: true,
  		width : 120,
  		valueField:'varIndex',
  		displayField : 'varName',
  		hiddenName:'storeId',
	    mode : 'remote',
  		store : ds_storeId_select,
  		selectOnFocus : true,
  		triggerAction : 'all',
  		valueNotFoundText:'',
  		loadingText : '加载中...',
  		editable:false,
  		emptyText : '请选择'
	  });
	  */
	 /* var source_comboBox= new Ext.form.ComboBox({
  		typeAhead: true,
  		width : 120,
  		valueField:'varIndex',
  		displayField : 'varName',
	    mode : 'remote',
	    fieldLabel:'来源',
  		hiddenName:'source',
  		store : ds_source_select,
  		selectOnFocus : true,
  		triggerAction : 'all',
  		allowBlank : false,
  		loadingText : '加载中...',
  		editable:false,
  		emptyText : '请选择'
	  });*/
		
	 	var receiveItem_browser_store=new Ext.data.JsonStore({
	 	    url:Uec3.App.Constants.homeUrl+"/receive/toReceive!fillReceiveItemBrowser.do",
			//params:{start:0,limit:2},
			remoteSort :true,
		    root: "root",
           idProperty: "id",
           totalProperty: "totalProperty", 
           pruneModifiedRecords:true,
           fields:['codeOfBuyer','code','name','source','measurementUnit','orderQuantity','quantity','cost','retailPrice','minPrice','insurancePrice',
                   'publicanLevel','brand','origin','storeId','storeName','shelf','carModel'],
           listeners :{
              beforeload:function(){
                 Ext.MessageBox.wait("正在加载数据...","提示");
              }
             
           }
	 	
	 	});
	 	
		 Ext.util.Format.comboRenderer = function(combo){
             return function(value){
            //var record = combo.findRecord(combo.valueField, value);
      		    var rowIndex = combo.getStore().find(combo.valueField, value);
      		    if(rowIndex<0) return '';   
                var record=combo.getStore().getAt(rowIndex); 
                return record ? record.get(combo.displayField) : combo.valueNotFoundText};
         }
		
		var receiveItem_browser_grid = new Ext.grid.GridPanel({
			    frame : false,
                columnLines :true,
                height:408,                   
                enableHdMenu : false,
                stripeRows: true,
                store:receiveItem_browser_store,
                //width : '100%',
                //autoWidth:true,
                //forceLayout :true,
  	   			 viewConfig:
  	   			 {
  	   			   //autoFill:true,
  	      		   //forceFit:true,
  	               scrollOffset:20
  	             },	   	   
  	             autoScroll : true,
  	             layout:'fit',
  	             //defaults:{width:180},
		         columns:[	
		             new Ext.grid.RowNumberer(),
		            {header:'单据号',dataIndex:'codeOfBuyer',sortable:true,width:130},
		            {header:'零件编码',dataIndex:'code',sortable:true,width:130},
		            {header:'零件名称',dataIndex:'name',width:130},
		            {header:'车型',dataIndex:'carModel',width:110},
		            {header:'来源',dataIndex:'source',width:100,renderer:Ext.util.Format.comboRenderer(source_comboBox)},
		            {header:'单位',dataIndex:'measurementUnit',width:110},
		            {header:'订购数量',dataIndex:'orderQuantity',width:120},
		            {header:'到货数量',dataIndex:'quantity',width:120},
		            {header:'进价',dataIndex:'cost',width:110},
		            {header:'媒体价',dataIndex:'retailPrice',width:110},
		            {header:'最低价',dataIndex:'minPrice',width:110},
		            {header:'保险价',dataIndex:'insurancePrice',width:110},
		            {header:'公开级别',dataIndex:'publicanLevel',width:130,renderer:Ext.util.Format.comboRenderer(publicanLevel_comboBox)},
		            {header:'品牌',dataIndex:'brand',width:120},
		            {header:'产地',dataIndex:'origin',width:120},
		            {header:'仓库',dataIndex:'storeName',width:120},
		            {header:'仓位',dataIndex:'shelf',width:120}	
		         ],
		         bbar: new Ext.PagingToolbar({
		            pageSize:20,
	                store:receiveItem_browser_store,
	                displayInfo: true,
	                displayMsg: '第 {0} - {1} 条  共 {2} 条',
                    emptyMsg: "没有记录",                    
                    listeners:{
                      beforechange:function(){
                      		 Ext.MessageBox.wait("正在加载数据!!!...","提示");	                     
                      },
                      change:function(){
                            Ext.MessageBox.hide();
                      }
                    }
		         })
		});
 		
		var view_panel=new Ext.FormPanel({
			  //applyTo: 'receive_item_browser',
		      // title:'浏览入库单明细',
		      //layout:'border',
			  //autoScroll:true,
			  border:true,
             // monitorResize: true,
		      items:[
		      	{
		          height:60,
		          layout:'hbox',
		          border:false,
		          layoutConfig: 
		             {     
		               padding:'5',
					   align:'middle'
                    },
		          items:[			             
		             {
		               xtype:'label',
		               text:'浏览入库单明细',
		               style: {    					 
     					  font:'bold 30px arial,serif'
   		               }
		             },{
		             	xtype:'button',
		             	text: '切换单头浏览方式',
		             	margins:'0 10',
		             	handler:function(){
		             		//openByUrl('${env.appUrl}/receive/toReceive!receiveBrowserExt.do','浏览入库单');
		            	    redirectUrl("receive_browser");
		             	}
		             },{
		                xtype:'spacer',
                       flex:1
		             },{
                       xtype:'textfield',
                       name:'code'
                    },{
                       xtype:'button',
                       text:'查找编码',                   
                       margins:'0 10',
                       handler:searchByCode
                    },{
                       xtype:'button',
                       text:'高级查找',
                       handler:function(){
                       		win.show();
                       }
                    },{
                       xtype:'spacer',
                       flex:1
                    },{
                       xtype:'button',
                       text: '新增采购进货',
                       handler:function(){
                           //openByUrl('$env.appUrl/receive/toReceive!toReceiveItemExt.do','采购入库单');
                    	 redirectUrl("receive_edit");
                       }
                    },{
                       xtype:'button',
                       text: '新增采购退货',
                       margins:'0 10',
                       handler:function(){
                    	   redirectUrl("returned_edit");
                    	   //openByUrl('$env.appUrl/returned/toReturned!toReturnedItemExt.do','采购退货单');
                       }
                    }
		          ]
		      },receiveItem_browser_grid			      	
		    ]  
		      
		});
		
		
		var advanced_search_form = new Ext.form.FormPanel({
		         labelSeparator : "：",
		         frame:true,
		         border:false,
		         autoHeight:true,
		         buttonAlign:'center',
		         labelWidth:55,
		         defaults:{width:120,anchor:'100%'},
		         items:[
		            {
		              xtype:'numberfield',
		              fieldLabel:'单据号',
		              name:'codeOfBuyer'
		            },{
		               xtype:'textfield',
		               fieldLabel:'编码',
		               name:'code' 
			          			     
		            },{
		               xtype:'textfield',
		               fieldLabel:'名称',
		               name:'name' 
		            },source_comboBox,{
		               xtype:'textfield',
		               fieldLabel:'品牌',
		               name:'brand'			               
		            },{
		               xtype:'textfield',
		               fieldLabel:'产地',
		               name:'origin' 
		            }
		         ],
		      buttons:[
		        { 
		         text:'查找',
				 handler:searchReceiveItemByCondition
		        },{
		     	  text:'关闭',
		     	  handler:function(){			     		
		     	    win.hide();
		     	  }
		        }
		      ]
			
		});
		
		var win = new Ext.Window({
			title:'高级查找',
		    layout:'fit',
	    	width:330,
	    	closeAction:'hide',
	    	autoHeight:true,
			resizable :true,
			shadow : true,
			modal :true,
	    	closable:true,
	    	bodyStyle:'padding:5 5 5 5',
	    	animCollapse:true,
			items:[advanced_search_form],
			listeners:{
			 'show':function(){
			  	  var form =  win.get(0).getForm();
			  	  if(form.el){
			     	 form.el.dom.reset();
		    	  }else{
			      	form.reset();
		    	  } 			  	 
			  }
			}
	    });
			
		 function searchByCode(){
	      var value=view_panel.getForm().findField("code").getValue();
	      if(!value){
	         Ext.MessageBox.alert("提示","请输入零件编码");
	      }else{
	         var queryParam=Ext.util.JSON.encode({code:value});		      
		     loadReceiveItem(queryParam);
	      }
	    }
	    
		function showSearchForm(){
			advanced_search_form.form.reset();
		    win.show();
		}
		
		function searchReceiveItemByCondition(){
		   var queryParam =Ext.util.JSON.encode(advanced_search_form.getForm().getValues());
		   loadReceiveItem(queryParam);
		   win.hide();
		}
		
		function loadReceiveItem(queryParam){
			if(Ext.isEmpty(queryParam)){
				 receiveItem_browser_store.baseParams = {start:0,limit:20};
			}else{
				 receiveItem_browser_store.baseParams = {start:0,limit:20,queryParam:queryParam};
			}	       
		    receiveItem_browser_store.load({callback:function(records,options,success){
	              if(success){
	              	 Ext.MessageBox.hide();
	                 var total=records.length;
	                 if(!total){
	                    Ext.MessageBox.alert("提示","未找到相关记录");
	                 }
	              }else{
	                 Ext.MessageBox.alert("提示",this.reader.jsonData.msg);
	              }
		    }});
	    }
	    
	    function init(){
	       ds_source_select.load();
	       ds_publicanLevel_select.load();	      
	    }		
	    init();
	    
		function redirectUrl(id,params){
		    var desktop =Uec3.App.getDesktop();   
		    desktop.launchWindowByParam(id,params); 	 		
	    }
	     	
		return view_panel;
   },
   /**
    * Initialize this module.
    * This function is called at startup (page load/refresh).
    */
   init : function(initParams){
   		
   },
	/**
	 * Create this modules window here.
	 */
   createWindow : function(){
 	
   	
      var desktop = this.app.getDesktop();
      var win = desktop.getWindow('receive_item_browser');
      if(!win){      	
         win = desktop.createWindow({
            id: 'receive_item_browser',
            title: '入库单明细浏览',
            width: 1000,
            height: 500,
            minWidth:785,
            minHeight:500,
            iconCls: 'acc-icon',
            shim: false,
            animCollapse: false,
            constrainHeader: true,
            maximizable: true,
            maximized:true,
            taskbuttonTooltip: "入库单明细浏览",           
            items:[this.getPanel()]
         });
      }
      win.get(0).get(1).getBottomToolbar().doRefresh();//刷新列表 
      win.get(0).get(1).setHeight(win.getHeight()-90);
      win.show(); 
      win.on('resize',function(win,width,height){
      	  win.get(0).get(1).setHeight(height-92);
      });
   }
 });
   